package com.wanshi.controller;

import com.github.pagehelper.PageInfo;
import com.wanshi.enitiy.NetImg;
import com.wanshi.service.NetImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Created by IntelliJ IDEA.
 * @Author jzy
 * @Date 2021/11/24 18:37
 */
@Controller
@RequestMapping("Net")
@PropertySource("classpath:netImg.properties")
public class NetImgController {

    @Autowired
    private NetImgService netImgService;

    @Value("${pic_save_path}")
    private String picSavePath;

    @RequestMapping("/index")
    public String index(){
         return "Net/index";
    }

    @RequestMapping("upload")
    public String upload(HttpServletRequest request, MultipartFile wenjian,Model model, NetImg netImg) throws IOException {
        String name = wenjian.getOriginalFilename();
        if(name.length()==0){
            model.addAttribute("error", "没有选择图片");
            return "Net/index";
        }
        String ip = request.getRemoteAddr();
        long size = wenjian.getSize();
        double dsize= size*1.0/1024;
        DecimalFormat df = new DecimalFormat("0.00");
        int lastIndex = name.lastIndexOf(".");
        String houzhui = name.substring(lastIndex);
        String replace = UUID.randomUUID().toString().replace("-", "")+houzhui;
        File file = new File(picSavePath);
        File fileName = new File(file,replace);
        wenjian.transferTo(fileName);

        netImg.setName(name);
        netImg.setSize(df.format(dsize)+"KB");
        netImg.setPicurl(replace);
        netImg.setUploadtime(new Date());
        netImg.setDescription(netImg.getDescription());
        netImg.setIp(ip);
        netImgService.save(netImg);
        return "redirect:index";
    }

    @RequestMapping("/findAllPage")
    public String findAllPage(Model model, @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum , @RequestParam(name = "pageSize",required = true,defaultValue = "3") int pageSize) {
        PageInfo pageInfo = netImgService.findAllPage(pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "Net/list";
    }

    @RequestMapping("/fileDownload")
    public ResponseEntity<byte[]> download(String fileName) throws IOException {
        File file = new File("D:/VUE/2021/11/02/"+fileName);
        byte[] body = null;
        InputStream is = new FileInputStream(file);
        body = new byte[is.available()];
        is.read(body);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + file.getName());
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;
    }
}
